import { z } from "zod";
import { VerificationTokenType } from "@/lib/enums";
import { TFunction } from "./common";

export const LoginSchema = (t: TFunction) =>
  z.object({
    email: z
      .email({
        message: t("emailInvalid"),
      })
      .min(1, { message: t("emailRequired") }),
    password: z.string().min(6, {
      message: t("passwordInvalid"),
    }),
  });

export type LoginFormData = z.infer<ReturnType<typeof LoginSchema>>;

export const RegisterSchema = (t: TFunction) =>
  z.object({
    email: z
      .email({
        message: t("emailInvalid"),
      })
      .min(1, { message: t("emailRequired") }),
  });

export type RegisterFormData = z.infer<ReturnType<typeof RegisterSchema>>;

export const RegisterVerificationSchema = (t: TFunction) =>
  z.object({
    type: z.enum(VerificationTokenType, {
      message: t("verificationTypeInvalid"),
    }),
    token: z.string().min(1, {
      message: t("verificationTokenRequired"),
    }),
    first_name: z.string().min(1, {
      message: t("firstNameRequired"),
    }),
    last_name: z.string().min(1, {
      message: t("lastNameRequired"),
    }),
    password: z.string().min(6, {
      message: t("passwordInvalid"),
    }),
    email: z
      .email({
        message: t("emailInvalid"),
      })
      .min(1, { message: t("emailRequired") }),
  });
export type RegisterVerificationFormData = z.infer<
  ReturnType<typeof RegisterVerificationSchema>
>;

export const ForgotSchema = (t: TFunction) =>
  z.object({
    email: z
      .email({
        message: t("emailInvalid"),
      })
      .min(1, { message: t("emailRequired") }),
  });

export type ForgotFormData = z.infer<ReturnType<typeof ForgotSchema>>;

export const ForgotVerificationSchema = (t: TFunction) =>
  z
    .object({
      email: z
        .email({
          message: t("emailInvalid"),
        })
        .min(1, { message: t("emailRequired") }),
      token: z.string().min(1, {
        message: t("verificationTokenRequired"),
      }),
      password: z.string().min(6, {
        message: t("passwordInvalid"),
      }),
      confirm_password: z.string().min(6, {
        message: t("confrimPasswordInvalid"),
      }),
    })
    .refine((data) => data.password === data.confirm_password, {
      message: t("passwordNotMatch"),
      path: ["confirm_password"],
    });

export type ForgotVerificationFormData = z.infer<
  ReturnType<typeof ForgotVerificationSchema>
>;
